<template>
  <el-submenu v-if="menu.list && menu.list.length >= 1" :index="menu.menuId + ''" class="submenu">
    <template slot="title">
      <span class="submenu-title">{{ menu.name }}</span>
    </template>
    <sub-menu
      v-for="item in menu.list" 
      :key="item.menuId"
      :menu="item"
      :dynamicMenuRoutes="dynamicMenuRoutes"
      class="submenu-item"
    ></sub-menu>
  </el-submenu>
  <el-menu-item v-else :index="menu.menuId + ''" @click="gotoRouteHandle(menu)" class="menu-item">
    <span class="menu-text">{{ menu.name }}</span>
  </el-menu-item>
</template>

<script>
import SubMenu from "./IndexAsideSub";
export default {
  name: "sub-menu",
  props: {
    menu: {
      type: Object,
      required: true
    },
    dynamicMenuRoutes: {
      type: Array,
      required: true
    }
  },
  components: {
    SubMenu
  },
  methods: {
    // 通过menuId与动态(菜单)路由进行匹配跳转至指定路由
    gotoRouteHandle(menu) {
      var route = this.dynamicMenuRoutes.filter(
        item => item.meta.menuId === menu.menuId
      );
      if (route.length >= 1) {
        if (route[0].component != null) {
          this.$router.replace({ name: route[0].name });
        } else {
          this.$router.push({ name: "404" });
        }
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.submenu {
  .submenu-title {
    font-size: 16px;
    color: #333;
    padding: 0 20px;
    transition: all 0.3s;
    
    &:hover {
      color: #409EFF;
    }
  }

  .submenu-item {
    padding-left: 20px;
  }
}

.menu-item {
  height: 50px;
  line-height: 50px;
  
  .menu-text {
    font-size: 14px;
    color: #666;
    padding: 0 20px;
    transition: all 0.3s;
    
    &:hover {
      color: #409EFF;
    }
  }

  &:hover {
    background: #f5f7fa;
  }
}
</style>
